﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class UserControls_TH_GioHang : System.Web.UI.UserControl
{
    private DataSet objShoppingCartDS;
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    // fill cart summary contents in the PreRender stage
    protected void Page_PreRender(object sender, EventArgs e)
    {
        PopulateControls();
    }
    // fill the controls with data
    private void PopulateControls()
    {
        // get the items in the shopping cart
        GioHangBUS objShoppingCart =
                    new GioHangBUS(TGDDConfiguration.DbConnectionString);
        try
        {
            objShoppingCartDS = objShoppingCart.GetItemsIntoGioHang(
                AccessGioHang.idGioHang);
            if (objShoppingCartDS.Tables[0].Rows.Count == 0)
            {
                lblCartSummary.Text = "Chưa có sản phẩm nào.";
                lblTotalAmount.Text = String.Format("{0:#,##0;Nothing}", 0) + " Đ";
                linkviewCart.Visible = false;
                list.Visible = false;
            }
            else
            {
                lblCartSummary.Visible = false;
                // populate the list with the shopping cart contents
                list.Visible = true;
                list.DataSource = objShoppingCartDS.Tables[0];
                list.DataBind();
                // set up controls
                linkviewCart.Visible = true;
                // display the total amount
                objShoppingCart = new GioHangBUS(
                   TGDDConfiguration.DbConnectionString);
                objShoppingCartDS = objShoppingCart.GetTotalAmountGioHang(
                    AccessGioHang.idGioHang);
                DataRow row = objShoppingCartDS.Tables[0].Rows[0];
                decimal amount = decimal.Parse(row["TongTien"].ToString());
                lblTotalAmount.Text = String.Format("{0:#,##0;Nothing}", amount) + " Đ";
            }
        }
        catch (System.Exception ex)
        {
            throw new Exception(ex.Message, ex.InnerException);
        }
    }

    // we don't want to display the cart summary in the shopping cart page
    protected void Page_Init(object sender, EventArgs e)
    {
        // get the current page
        string page = Request.AppRelativeCurrentExecutionFilePath;
        // if we're in the shopping cart, don't display the cart summary
        if (String.Compare(page, "~/GioHang.aspx", true) == 0)
            this.Visible = false;
        else
            this.Visible = true;
    }
}
